<!DOCTYPE html>
<html>

  <head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

  <title>安卓自定义View进阶-特殊控件的事件处理方案</title>
  <meta name="description" content="本文带大家了解 Android 特殊形状控件的事件处理方式，主要是利用了 Region 和 Matrix 的一些方法，超级实用的事件处理方案，相信看完本篇之后，任何奇葩控件的事件处理都会变得十分简单。">
  <meta name="author" content="GcsSloop">
  <meta name="keywords" content="Region,Matrix,特殊控件的事件处理,自定义View详解, 自定义控件, 安卓, Android, CustomView, GcsSloop">
  <meta name="关键字" content="Region,Matrix,特殊控件的事件处理,自定义View详解, 自定义控件, 安卓, Android, CustomView, GcsSloop">
  

  <meta name="twitter:card" content="summary">
  <meta name="twitter:title" content="安卓自定义View进阶-特殊控件的事件处理方案">
  <meta name="twitter:description" content="本文带大家了解 Android 特殊形状控件的事件处理方式，主要是利用了 Region 和 Matrix 的一些方法，超级实用的事件处理方案，相信看完本篇之后，任何奇葩控件的事件处理都会变得十分简单。">
  <meta name="twitter:keywords" content="Region,Matrix,特殊控件的事件处理,自定义View详解, 自定义控件, 安卓, Android, CustomView, GcsSloop">
  
  <meta property="og:type" content="article">
  <meta property="og:title" content="安卓自定义View进阶-特殊控件的事件处理方案">
  <meta property="og:description" content="本文带大家了解 Android 特殊形状控件的事件处理方式，主要是利用了 Region 和 Matrix 的一些方法，超级实用的事件处理方案，相信看完本篇之后，任何奇葩控件的事件处理都会变得十分简单。">
  <meta name="og:keywords" content="Region,Matrix,特殊控件的事件处理,自定义View详解, 自定义控件, 安卓, Android, CustomView, GcsSloop">

  <meta name="theme-color" content="#343434">
  
  <link rel="icon" type="image/png" href="https://raw.githubusercontent.com/GcsSloop/gcssloop.github.io/master/assets/siteinfo/favicon.png" />
  <link href="https://raw.githubusercontent.com/GcsSloop/gcssloop.github.io/master/assets/siteinfo/favicon.png" rel="shortcut icon" type="image/png">
  
  <link href="//netdna.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css" rel="stylesheet">
  <link rel="stylesheet" href="/css/main.css">

  <link rel="canonical" href="http://www.gcssloop.com/customview/touch-matrix-region">
  <link rel="alternate" type="application/rss+xml" title="GcsSloop" href="http://www.gcssloop.com/feed.xml">
  
  <meta name="google-site-verification" content="Z_g58PkzRAyBMxkqrcDdWrTBK8oOWM-7rUHauhLNF2E" />
  <meta name="baidu-site-verification" content="kUtTXCKaZs" />
  <meta name="baidu-site-verification" content="6DuDv3aaJX" />
  
  <!--阅读次数统计-->
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"> </script>
  
  <!--Fuck Weixin and Baidu-->
  <meta http-equiv="Cache-Control" content="no-transform">
  <meta http-equiv=”Cache-Control” content=”no-siteapp” />
  <meta name="applicable-device" content="pc,mobile">
  <meta name="HandheldFriendly" content="true"/>

  <!-- Google Ad -->
  <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
  <script>
    (adsbygoogle = window.adsbygoogle || []).push({
      google_ad_client: "ca-pub-2767831356529421",
      enable_page_level_ads: true
    });
  </script>

</head>


  <body>

    <span class="mobile btn-mobile-menu">
        <i class="fa fa-list btn-mobile-menu__icon"></i>
        <i class="fa fa-angle-up btn-mobile-close__icon hidden"></i>
    </span>
    
    <header class="panel-cover panel-cover--collapsed" style="background-image: url('/assets/siteinfo/background-cover.jpg')">
  <div class="panel-main">

    <div class="panel-main__inner panel-inverted">
    <div class="panel-main__content">

        <a href="/#blog" title="前往 GcsSloop 的主页" class="blog-button"><img src="/assets/siteinfo/avatar.jpg" width="80" alt="GcsSloop logo" class="panel-cover__logo logo" /></a>
        <h1 class="panel-cover__title panel-title"><a href="/#blog" title="link to homepage for GcsSloop" class="blog-button">GcsSloop</a></h1>

        
        <span class="panel-cover__subtitle panel-subtitle">Just do IT later.</span>
        
        <hr class="panel-cover__divider" />
        <p class="panel-cover__description">嗨，我是 GcsSloop，一名来自2.5次元的魔法师，Android自定义View系列文章作者，非著名程序员。</p>
        <hr class="panel-cover__divider panel-cover__divider--secondary" />
        
        
        <p class="panel-cover__description">欢迎来到我的魔法世界!</p>
        
        
        <div class="navigation-wrapper">
          <div>
            <nav class="cover-navigation cover-navigation--primary">
              <ul class="navigation">
                <li class="navigation__item"><a href="/#blog" title="访问博客" class="blog-button">博客</a></li>
                
                  
                    <li class="navigation__item"><a href="https://github.com/GcsSloop" target="_blank" title="GcsSloop's GitHub">GitHub</a></li>
                  
                  
                
                  
                    <li class="navigation__item"><a href="/timeline" title="博客目录">目录</a></li>
                  
                  
                
                  
                    <li class="navigation__item"><a href="https://xiaozhuanlan.com/u/GcsSloop" target="_blank" title="小专栏">专栏</a></li>
                  
                  
                
                  
                    <li class="navigation__item"><a href="/category/customview" title="自定义View教程目录">自定义控件</a></li>
                  
                  
                
                  
                    <li class="navigation__item"><a href="/friends" title="友链">友链</a></li>
                  
                  
                
              </ul>
            </nav>
          </div>
          
          <div><nav class="cover-navigation navigation--social">
  <ul class="navigation">

  
  <!-- Weibo -->
  <li class="navigation__item">
    <a href="http://weibo.com/GcsSloop" title="@GcsSloop 的微博" target="_blank">
      <i class='social fa fa-weibo'></i>
      <span class="label">Weibo</span>
    </a>
  </li>
  

  
  <!-- Github -->
  <li class="navigation__item">
    <a href="https://github.com/GcsSloop" title="@GcsSloop 的 Github" target="_blank">
      <i class='social fa fa-github'></i>
      <span class="label">Github</span>
    </a>
  </li>
  
  
  
  <!-- Twitter -->
  <li class="navigation__item">
    <a href="http://twitter.com/GcsSloop" title="@GcsSloop" target="_blank">
      <i class='social fa fa-twitter'></i>
      <span class="label">Twitter</span>
    </a>
  </li>
  

    

  

  
  <!-- RSS -->
  <li class="navigation__item">
    <a href="/feed.xml" rel="author" title="RSS" target="_blank">
      <i class='social fa fa-rss'></i>
      <span class="label">RSS</span>
    </a>
  </li>
  

  
  <!-- Email -->
  <li class="navigation__item">
    <a href="mailto:GcsSloop@gmail.com" title="发邮件给我">
      <i class='social fa fa-envelope'></i>
      <span class="label">Email</span>
    </a>
  </li>
  

  
  <!-- Copyright -->
  <li class="navigation__item">
    <a href="http://choosealicense.online" title="选择版权"  target="_blank">
      <i class="social fa fa-copyright"></i>
      <span class="label">版权</span>
    </a>
  </li>
  
  
  </ul>
</nav>
</div>
        </div>
      </div>
    </div>
    
    
    <div class="panel-cover--overlay cover-slate"></div>
    
  </div>
</header>


    <div class="content-wrapper">
        <div class="content-wrapper__inner">
            <article class="post-container post-container--single" itemscope itemtype="http://schema.org/BlogPosting">
  <header class="post-header">
    <div class="post-meta" style="font-size:.8em">
      <time datetime="2016-11-16 00:00:00 +0800" itemprop="datePublished" class="post-meta__date date">2016-11-16</time> &#8226; <span class="post-meta__tags tags">Android,Region,Matrix</span> &#8226; View <span id="busuanzi_value_page_pv"></span> times.
</span>
    </div>
    <h1 class="post-title">安卓自定义View进阶-特殊控件的事件处理方案</h1>
  </header>

  <section class="post">
    <p>本文带大家了解 Android 特殊形状控件的事件处理方式，主要是利用了 Region 和 Matrix 的一些方法，超级实用的事件处理方案，相信看完本篇之后，任何奇葩控件的事件处理都会变得十分简单。</p>

<p>不得不说，Android 对事件体系封装的非常棒，即便对事件体系不太了解的人，只要简单的调用方法就能使用，而且具有防呆设计，能够保证事件流的完整性和统一性，最大可能性的避免了事件处理的混乱，着实令人佩服。<br />
<strong>然而世界上并没有绝对完美的东西，当”事件处理”遇上”自定义View”，一场好戏就开演了。</strong></p>

<h2 id="特殊形状控件">特殊形状控件</h2>

<p>在通常的情况下，自定义 View 直接使用系统的事件体系处理就行，我们也不需要特殊处理，然而当一些特殊的控件出现的时候，麻烦就来了，举个栗子：</p>

<p><img src="http://gcsblog.oss-cn-shanghai.aliyuncs.com/blog/2019-04-29-071510.jpg?gcssloop" alt="" /></p>

<p>这是一个在遥控器上非常常见的按键布局，注意中间上下左右选择的部分，看起来十分简单，然而当你真正准备在手机上实现的时候麻烦就出现了。因为所有的 View 默认都是矩形的，所以事件接收区域也是矩形的，如果直接使用系统提供的 View 来组合出一摸一样的布局也很简单，但点击区域该如何处理？显然有部分点击区域是在控件外面的，并且会产生重叠区域：</p>

<blockquote>
  <p>红色方框表示 View 的可点击区域。</p>
</blockquote>

<p><img src="http://gcsblog.oss-cn-shanghai.aliyuncs.com/blog/2019-04-29-071511.jpg?gcssloop" alt="" /></p>

<p>当我们面对这样比较奇特的控件的时候，有很多处理办法，比较投机的一种就是背景贴一个静态图，按钮做成透明的，设置小一点，放在对应的位置，这样可以保证不会误触，当然了如果想要点击效果可以在按钮按下的时候更新一下背景图，这样虽然也可以，但是这样会导致可点击区域变小，体验效果变差，设计方案变得复杂，而且逻辑也不容易处理，是一种非常糟糕的设计。</p>

<p>当然了，看了我这么多文章的小伙伴应该也猜到接下来要说什么了，没错，就是自定义 View。当我们面对一些奇葩控件的时候，自定义 View 就变成了一种非常好用的处理方案。</p>

<p>相信小伙伴们看过 <a href="http://www.gcssloop.com/customview/CustomViewIndex">前面的文章</a> 之后，对各种图形的绘制已经不成问题了，所以我们直接处理重点问题。</p>

<blockquote>
  <h4 id="注意">注意：</h4>

  <p>本文中所有的 自定义View 均继承自 CustomView ，这是一个自定义的超类，目的是简化 自定义View 部分常用操作，你可以在 <a href="https://github.com/GcsSloop/ViewSupport/wiki/CustomView">ViewSupport</a> 中找到它以及关于它的简介。<br />
<strong>⚠️ 警告：测试本文章示例之前请关闭硬件加速。</strong></p>
</blockquote>

<h2 id="特殊形状控的点击区域判断">特殊形状控的点击区域判断</h2>

<p>要进行特殊形状的点击判断，要用到一个之前没有使用过的类：Region。</p>

<p>Region 直接翻译的意思是 地域，区域。<strong>在此处应该是区域的意思</strong>。它和 Path 有些类似，但 Path 可以是不封闭图形，而 Region 总是封闭的。可以通过 <code class="highlighter-rouge">setPath</code> 方法将 Path 转换为 Region。</p>

<p><strong>本文中我们重点要使用到的是 Region 中的 <code class="highlighter-rouge">contains</code> 方法，这个方法可以判断一个点是否包含在该区域内。</strong></p>

<p>接下来是一个简单的示例，<strong>判断手指是否是在圆形区域内按下</strong>:</p>

<p><img src="http://gcsblog.oss-cn-shanghai.aliyuncs.com/blog/2019-04-29-071512.jpg?gcssloop" alt="" /></p>

<p>代码：</p>

<div class="language-java highlighter-rouge"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">RegionClickView</span> <span class="kd">extends</span> <span class="n">CustomView</span> <span class="o">{</span>
    <span class="n">Region</span> <span class="n">circleRegion</span><span class="o">;</span>
    <span class="n">Path</span> <span class="n">circlePath</span><span class="o">;</span>

    <span class="kd">public</span> <span class="nf">RegionClickView</span><span class="o">(</span><span class="n">Context</span> <span class="n">context</span><span class="o">)</span> <span class="o">{</span>
        <span class="kd">super</span><span class="o">(</span><span class="n">context</span><span class="o">);</span>
        <span class="n">mDeafultPaint</span><span class="o">.</span><span class="na">setColor</span><span class="o">(</span><span class="mi">0</span><span class="n">xFF4E5268</span><span class="o">);</span>
        <span class="n">circlePath</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Path</span><span class="o">();</span>
        <span class="n">circleRegion</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Region</span><span class="o">();</span>
    <span class="o">}</span>

    <span class="nd">@Override</span>
    <span class="kd">protected</span> <span class="kt">void</span> <span class="nf">onSizeChanged</span><span class="o">(</span><span class="kt">int</span> <span class="n">w</span><span class="o">,</span> <span class="kt">int</span> <span class="n">h</span><span class="o">,</span> <span class="kt">int</span> <span class="n">oldw</span><span class="o">,</span> <span class="kt">int</span> <span class="n">oldh</span><span class="o">)</span> <span class="o">{</span>
        <span class="kd">super</span><span class="o">.</span><span class="na">onSizeChanged</span><span class="o">(</span><span class="n">w</span><span class="o">,</span> <span class="n">h</span><span class="o">,</span> <span class="n">oldw</span><span class="o">,</span> <span class="n">oldh</span><span class="o">);</span>
      	<span class="c1">// ▼在屏幕中间添加一个圆</span>
        <span class="n">circlePath</span><span class="o">.</span><span class="na">addCircle</span><span class="o">(</span><span class="n">w</span><span class="o">/</span><span class="mi">2</span><span class="o">,</span> <span class="n">h</span><span class="o">/</span><span class="mi">2</span><span class="o">,</span> <span class="mi">300</span><span class="o">,</span> <span class="n">Path</span><span class="o">.</span><span class="na">Direction</span><span class="o">.</span><span class="na">CW</span><span class="o">);</span>
        <span class="c1">// ▼将剪裁边界设置为视图大小</span>
        <span class="n">Region</span> <span class="n">globalRegion</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Region</span><span class="o">(-</span><span class="n">w</span><span class="o">,</span> <span class="o">-</span><span class="n">h</span><span class="o">,</span> <span class="n">w</span><span class="o">,</span> <span class="n">h</span><span class="o">);</span>
        <span class="c1">// ▼将 Path 添加到 Region 中</span>
        <span class="n">circleRegion</span><span class="o">.</span><span class="na">setPath</span><span class="o">(</span><span class="n">circlePath</span><span class="o">,</span> <span class="n">globalRegion</span><span class="o">);</span>
    <span class="o">}</span>

    <span class="nd">@Override</span>
    <span class="kd">public</span> <span class="kt">boolean</span> <span class="nf">onTouchEvent</span><span class="o">(</span><span class="n">MotionEvent</span> <span class="n">event</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">switch</span> <span class="o">(</span><span class="n">event</span><span class="o">.</span><span class="na">getAction</span><span class="o">()){</span>
            <span class="k">case</span> <span class="n">MotionEvent</span><span class="o">.</span><span class="na">ACTION_DOWN</span><span class="o">:</span>
                <span class="kt">int</span> <span class="n">x</span> <span class="o">=</span> <span class="o">(</span><span class="kt">int</span><span class="o">)</span> <span class="n">event</span><span class="o">.</span><span class="na">getX</span><span class="o">();</span>
                <span class="kt">int</span> <span class="n">y</span> <span class="o">=</span> <span class="o">(</span><span class="kt">int</span><span class="o">)</span> <span class="n">event</span><span class="o">.</span><span class="na">getY</span><span class="o">();</span>

                <span class="c1">// ▼点击区域判断</span>
                <span class="k">if</span> <span class="o">(</span><span class="n">circleRegion</span><span class="o">.</span><span class="na">contains</span><span class="o">(</span><span class="n">x</span><span class="o">,</span><span class="n">y</span><span class="o">)){</span>
                    <span class="n">Toast</span><span class="o">.</span><span class="na">makeText</span><span class="o">(</span><span class="k">this</span><span class="o">.</span><span class="na">getContext</span><span class="o">(),</span><span class="s">"圆被点击"</span><span class="o">,</span><span class="n">Toast</span><span class="o">.</span><span class="na">LENGTH_SHORT</span><span class="o">).</span><span class="na">show</span><span class="o">();</span>
                <span class="o">}</span>
                <span class="k">break</span><span class="o">;</span>
        <span class="o">}</span>
        <span class="k">return</span> <span class="kc">true</span><span class="o">;</span>
    <span class="o">}</span>

    <span class="nd">@Override</span>
    <span class="kd">protected</span> <span class="kt">void</span> <span class="nf">onDraw</span><span class="o">(</span><span class="n">Canvas</span> <span class="n">canvas</span><span class="o">)</span> <span class="o">{</span>
        <span class="c1">// ▼注意此处将全局变量转化为局部变量，方便 GC 回收 canvas</span>
        <span class="n">Path</span> <span class="n">circle</span> <span class="o">=</span> <span class="n">circlePath</span><span class="o">;</span>
        <span class="c1">// 绘制圆</span>
        <span class="n">canvas</span><span class="o">.</span><span class="na">drawPath</span><span class="o">(</span><span class="n">circle</span><span class="o">,</span><span class="n">mDeafultPaint</span><span class="o">);</span>
    <span class="o">}</span>
<span class="o">}</span>
</code></pre>
</div>

<blockquote>
  <p>代码中比较重要的内容都用 ▼ 符号标记出来了。</p>
</blockquote>

<p>上述代码非常简单，就是创建了个 Path 并在其中添加圆形，之后将 Path 设置到 Region 中，当手指在屏幕上按下的时候判断手指位置是否在 Region 区域内。</p>

<h2 id="画布变换后坐标转换问题">画布变换后坐标转换问题</h2>

<p>还是本文一开始的例子，绘制一个上下左右选择按键，这个控件是上下左右对称的，熟悉我代码风格的小伙伴都知道，如果遇上这种问题，我肯定是要将坐标系平移到这个控件中心的，这样数据比较好计算，然而进行画布变换操作会产生一个新问题：<strong>手指触摸的坐标系和画布坐标系不统一，就可能引起手指触摸位置和绘制位置不统一。</strong></p>

<p>举个栗子：</p>

<blockquote>
  <p>画布移动后在手指按下位置绘制一个圆，可以看到，直接拿手指触摸位置的坐标来绘制会导致绘制位置不正确，<strong>两者坐标是相同的，但是由于坐标系不同，导致实际显示位置不同。</strong></p>
</blockquote>

<p><img src="http://gcsblog.oss-cn-shanghai.aliyuncs.com/blog/2019-04-29-71513.jpg?gcssloop" alt="" /></p>

<p>代码：</p>

<div class="language-java highlighter-rouge"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">CanvasVonvertTouchTest</span> <span class="kd">extends</span> <span class="n">CustomView</span><span class="o">{</span>
    <span class="kt">float</span> <span class="n">down_x</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span><span class="o">;</span>
    <span class="kt">float</span> <span class="n">down_y</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span><span class="o">;</span>

    <span class="kd">public</span> <span class="nf">CanvasVonvertTouchTest</span><span class="o">(</span><span class="n">Context</span> <span class="n">context</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">this</span><span class="o">(</span><span class="n">context</span><span class="o">,</span> <span class="kc">null</span><span class="o">);</span>
    <span class="o">}</span>

    <span class="kd">public</span> <span class="nf">CanvasVonvertTouchTest</span><span class="o">(</span><span class="n">Context</span> <span class="n">context</span><span class="o">,</span> <span class="n">AttributeSet</span> <span class="n">attrs</span><span class="o">)</span> <span class="o">{</span>
        <span class="kd">super</span><span class="o">(</span><span class="n">context</span><span class="o">,</span> <span class="n">attrs</span><span class="o">);</span>
    <span class="o">}</span>

    <span class="nd">@Override</span>
    <span class="kd">public</span> <span class="kt">boolean</span> <span class="nf">onTouchEvent</span><span class="o">(</span><span class="n">MotionEvent</span> <span class="n">event</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">switch</span> <span class="o">(</span><span class="n">event</span><span class="o">.</span><span class="na">getActionMasked</span><span class="o">()){</span>
            <span class="k">case</span> <span class="n">MotionEvent</span><span class="o">.</span><span class="na">ACTION_DOWN</span><span class="o">:</span>
            <span class="k">case</span> <span class="n">MotionEvent</span><span class="o">.</span><span class="na">ACTION_MOVE</span><span class="o">:</span>
                <span class="n">down_x</span> <span class="o">=</span> <span class="n">event</span><span class="o">.</span><span class="na">getX</span><span class="o">();</span>
                <span class="n">down_y</span> <span class="o">=</span> <span class="n">event</span><span class="o">.</span><span class="na">getY</span><span class="o">();</span>
                <span class="n">invalidate</span><span class="o">();</span>
                <span class="k">break</span><span class="o">;</span>

            <span class="k">case</span> <span class="n">MotionEvent</span><span class="o">.</span><span class="na">ACTION_CANCEL</span><span class="o">:</span>
            <span class="k">case</span> <span class="n">MotionEvent</span><span class="o">.</span><span class="na">ACTION_UP</span><span class="o">:</span>
                <span class="n">down_x</span> <span class="o">=</span> <span class="n">down_y</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span><span class="o">;</span>
                <span class="n">invalidate</span><span class="o">();</span>
                <span class="k">break</span><span class="o">;</span>
        <span class="o">}</span>

        <span class="k">return</span> <span class="kc">true</span><span class="o">;</span>
    <span class="o">}</span>

    <span class="nd">@Override</span>
    <span class="kd">protected</span> <span class="kt">void</span> <span class="nf">onDraw</span><span class="o">(</span><span class="n">Canvas</span> <span class="n">canvas</span><span class="o">)</span> <span class="o">{</span>
        <span class="kt">float</span> <span class="n">x</span> <span class="o">=</span> <span class="n">down_x</span><span class="o">;</span>
        <span class="kt">float</span> <span class="n">y</span> <span class="o">=</span> <span class="n">down_y</span><span class="o">;</span>

        <span class="n">drawTouchCoordinateSpace</span><span class="o">(</span><span class="n">canvas</span><span class="o">);</span>       <span class="c1">// 绘制触摸坐标系 灰色</span>
		
      	<span class="c1">// ▼注意画布平移</span>
        <span class="n">canvas</span><span class="o">.</span><span class="na">translate</span><span class="o">(</span><span class="n">mViewWidth</span><span class="o">/</span><span class="mi">2</span><span class="o">,</span> <span class="n">mViewHeight</span><span class="o">/</span><span class="mi">2</span><span class="o">);</span>  
        
        <span class="n">drawTranslateCoordinateSpace</span><span class="o">(</span><span class="n">canvas</span><span class="o">);</span>	 <span class="c1">// 绘制平移后的坐标系，红色</span>

        <span class="k">if</span> <span class="o">(</span><span class="n">x</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span> <span class="o">&amp;&amp;</span> <span class="n">y</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="o">)</span> <span class="k">return</span><span class="o">;</span>          <span class="c1">// 如果没有就返回</span>

        <span class="n">canvas</span><span class="o">.</span><span class="na">drawCircle</span><span class="o">(</span><span class="n">x</span><span class="o">,</span><span class="n">y</span><span class="o">,</span><span class="mi">20</span><span class="o">,</span><span class="n">mDeafultPaint</span><span class="o">);</span> <span class="c1">// 在触摸位置绘制一个小圆</span>
    <span class="o">}</span>

    <span class="cm">/**
     *  绘制触摸坐标系，灰色，为了能够显示出坐标系，将坐标系位置稍微偏移了一点
     */</span>
    <span class="kd">private</span> <span class="kt">void</span> <span class="nf">drawTouchCoordinateSpace</span><span class="o">(</span><span class="n">Canvas</span> <span class="n">canvas</span><span class="o">)</span> <span class="o">{</span>
        <span class="n">canvas</span><span class="o">.</span><span class="na">save</span><span class="o">();</span>
        <span class="n">canvas</span><span class="o">.</span><span class="na">translate</span><span class="o">(</span><span class="mi">10</span><span class="o">,</span><span class="mi">10</span><span class="o">);</span>
        <span class="n">CanvasAidUtils</span><span class="o">.</span><span class="na">set2DAxisLength</span><span class="o">(</span><span class="mi">1000</span><span class="o">,</span> <span class="mi">0</span><span class="o">,</span> <span class="mi">1400</span><span class="o">,</span> <span class="mi">0</span><span class="o">);</span>
        <span class="n">CanvasAidUtils</span><span class="o">.</span><span class="na">setLineColor</span><span class="o">(</span><span class="n">Color</span><span class="o">.</span><span class="na">GRAY</span><span class="o">);</span>
        <span class="n">CanvasAidUtils</span><span class="o">.</span><span class="na">draw2DCoordinateSpace</span><span class="o">(</span><span class="n">canvas</span><span class="o">);</span>
        <span class="n">canvas</span><span class="o">.</span><span class="na">restore</span><span class="o">();</span>
    <span class="o">}</span>

    <span class="cm">/**
     * 绘制平移后的坐标系，红色
     */</span>
    <span class="kd">private</span> <span class="kt">void</span> <span class="nf">drawTranslateCoordinateSpace</span><span class="o">(</span><span class="n">Canvas</span> <span class="n">canvas</span><span class="o">)</span> <span class="o">{</span>
        <span class="n">CanvasAidUtils</span><span class="o">.</span><span class="na">set2DAxisLength</span><span class="o">(</span><span class="mi">500</span><span class="o">,</span> <span class="mi">500</span><span class="o">,</span> <span class="mi">700</span><span class="o">,</span> <span class="mi">700</span><span class="o">);</span>
        <span class="n">CanvasAidUtils</span><span class="o">.</span><span class="na">setLineColor</span><span class="o">(</span><span class="n">Color</span><span class="o">.</span><span class="na">RED</span><span class="o">);</span>
        <span class="n">CanvasAidUtils</span><span class="o">.</span><span class="na">draw2DCoordinateSpace</span><span class="o">(</span><span class="n">canvas</span><span class="o">);</span>
        <span class="n">CanvasAidUtils</span><span class="o">.</span><span class="na">draw2DCoordinateSpace</span><span class="o">(</span><span class="n">canvas</span><span class="o">);</span>
    <span class="o">}</span>
<span class="o">}</span>
</code></pre>
</div>

<p><strong>那么问题来了，我们在之前的文章中讲过，映射不同坐标系的坐标用 什么来着？</strong><br />
<strong>是 Matrix。</strong></p>

<p>如果看过我之前的文章但没有想起来的说明你们根本没有认真看，全部拖出去糟蹋 5 分钟！<br />
没看过的点 <a href="http://www.gcssloop.com/customview/Matrix_Basic">Matrix原理</a> 和  <a href="http://www.gcssloop.com/customview/Matrix_Method">Matrix详解</a> 。</p>

<blockquote>
  <p><strong>Matrix 是一个矩阵，主要功能是坐标映射，数值转换。</strong></p>
</blockquote>

<p>那么接下来我们就对上面的示例进行简单的改造一下，让触摸位置和实际绘制绘制重合。小白点和黑色的圆没有完全重合是因为系统显示触摸位置的绘制逻辑和我使用的绘制逻辑不太相同导致的。</p>

<p><img src="http://gcsblog.oss-cn-shanghai.aliyuncs.com/blog/2019-04-29-71519.jpg?gcssloop" alt="" /></p>

<p>代码：</p>

<p><strong>注意：比较重要的修改位置用▼标记出来了。</strong></p>

<div class="language-java highlighter-rouge"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">CanvasVonvertTouchTest</span> <span class="kd">extends</span> <span class="n">CustomView</span><span class="o">{</span>
    <span class="kt">float</span> <span class="n">down_x</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span><span class="o">;</span>
    <span class="kt">float</span> <span class="n">down_y</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span><span class="o">;</span>

    <span class="kd">public</span> <span class="nf">CanvasVonvertTouchTest</span><span class="o">(</span><span class="n">Context</span> <span class="n">context</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">this</span><span class="o">(</span><span class="n">context</span><span class="o">,</span> <span class="kc">null</span><span class="o">);</span>
    <span class="o">}</span>

    <span class="kd">public</span> <span class="nf">CanvasVonvertTouchTest</span><span class="o">(</span><span class="n">Context</span> <span class="n">context</span><span class="o">,</span> <span class="n">AttributeSet</span> <span class="n">attrs</span><span class="o">)</span> <span class="o">{</span>
        <span class="kd">super</span><span class="o">(</span><span class="n">context</span><span class="o">,</span> <span class="n">attrs</span><span class="o">);</span>
    <span class="o">}</span>

    <span class="nd">@Override</span>
    <span class="kd">public</span> <span class="kt">boolean</span> <span class="nf">onTouchEvent</span><span class="o">(</span><span class="n">MotionEvent</span> <span class="n">event</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">switch</span> <span class="o">(</span><span class="n">event</span><span class="o">.</span><span class="na">getActionMasked</span><span class="o">()){</span>
            <span class="k">case</span> <span class="n">MotionEvent</span><span class="o">.</span><span class="na">ACTION_DOWN</span><span class="o">:</span>
            <span class="k">case</span> <span class="n">MotionEvent</span><span class="o">.</span><span class="na">ACTION_MOVE</span><span class="o">:</span>
                <span class="c1">// ▼ 注意此处使用 getRawX，而不是 getX</span>
                <span class="n">down_x</span> <span class="o">=</span> <span class="n">event</span><span class="o">.</span><span class="na">getRawX</span><span class="o">();</span>
                <span class="n">down_y</span> <span class="o">=</span> <span class="n">event</span><span class="o">.</span><span class="na">getRawY</span><span class="o">();</span>
                <span class="n">invalidate</span><span class="o">();</span>
                <span class="k">break</span><span class="o">;</span>

            <span class="k">case</span> <span class="n">MotionEvent</span><span class="o">.</span><span class="na">ACTION_CANCEL</span><span class="o">:</span>
            <span class="k">case</span> <span class="n">MotionEvent</span><span class="o">.</span><span class="na">ACTION_UP</span><span class="o">:</span>
                <span class="n">down_x</span> <span class="o">=</span> <span class="n">down_y</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span><span class="o">;</span>
                <span class="n">invalidate</span><span class="o">();</span>
                <span class="k">break</span><span class="o">;</span>
        <span class="o">}</span>

        <span class="k">return</span> <span class="kc">true</span><span class="o">;</span>
    <span class="o">}</span>

    <span class="nd">@Override</span>
    <span class="kd">protected</span> <span class="kt">void</span> <span class="nf">onDraw</span><span class="o">(</span><span class="n">Canvas</span> <span class="n">canvas</span><span class="o">)</span> <span class="o">{</span>
        <span class="kt">float</span><span class="o">[]</span> <span class="n">pts</span> <span class="o">=</span> <span class="o">{</span><span class="n">down_x</span><span class="o">,</span> <span class="n">down_y</span><span class="o">};</span>

        <span class="n">drawTouchCoordinateSpace</span><span class="o">(</span><span class="n">canvas</span><span class="o">);</span>            <span class="c1">// 绘制触摸坐标系，灰色</span>
        <span class="c1">// ▼注意画布平移</span>
        <span class="n">canvas</span><span class="o">.</span><span class="na">translate</span><span class="o">(</span><span class="n">mViewWidth</span><span class="o">/</span><span class="mi">2</span><span class="o">,</span> <span class="n">mViewHeight</span><span class="o">/</span><span class="mi">2</span><span class="o">);</span>  
        
        <span class="n">drawTranslateCoordinateSpace</span><span class="o">(</span><span class="n">canvas</span><span class="o">);</span>        <span class="c1">// 绘制平移后的坐标系，红色</span>
        
        <span class="k">if</span> <span class="o">(</span><span class="n">pts</span><span class="o">[</span><span class="mi">0</span><span class="o">]</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span> <span class="o">&amp;&amp;</span> <span class="n">pts</span><span class="o">[</span><span class="mi">1</span><span class="o">]</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="o">)</span> <span class="k">return</span><span class="o">;</span>    <span class="c1">// 如果没有就返回</span>

        <span class="c1">// ▼ 获得当前矩阵的逆矩阵</span>
        <span class="n">Matrix</span> <span class="n">invertMatrix</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Matrix</span><span class="o">();</span>
        <span class="n">canvas</span><span class="o">.</span><span class="na">getMatrix</span><span class="o">().</span><span class="na">invert</span><span class="o">(</span><span class="n">invertMatrix</span><span class="o">);</span>

        <span class="c1">// ▼ 使用 mapPoints 将触摸位置转换为画布坐标</span>
        <span class="n">invertMatrix</span><span class="o">.</span><span class="na">mapPoints</span><span class="o">(</span><span class="n">pts</span><span class="o">);</span>

        <span class="c1">// 在触摸位置绘制一个小圆</span>
        <span class="n">canvas</span><span class="o">.</span><span class="na">drawCircle</span><span class="o">(</span><span class="n">pts</span><span class="o">[</span><span class="mi">0</span><span class="o">],</span><span class="n">pts</span><span class="o">[</span><span class="mi">1</span><span class="o">],</span><span class="mi">20</span><span class="o">,</span><span class="n">mDeafultPaint</span><span class="o">);</span>
    <span class="o">}</span>

    <span class="cm">/**
     *  绘制触摸坐标系，颜色为灰色，为了能够显示出坐标系，将坐标系位置稍微偏移了一点
     */</span>
    <span class="kd">private</span> <span class="kt">void</span> <span class="nf">drawTouchCoordinateSpace</span><span class="o">(</span><span class="n">Canvas</span> <span class="n">canvas</span><span class="o">)</span> <span class="o">{</span>
        <span class="n">canvas</span><span class="o">.</span><span class="na">save</span><span class="o">();</span>
        <span class="n">canvas</span><span class="o">.</span><span class="na">translate</span><span class="o">(</span><span class="mi">10</span><span class="o">,</span><span class="mi">10</span><span class="o">);</span>
        <span class="n">CanvasAidUtils</span><span class="o">.</span><span class="na">set2DAxisLength</span><span class="o">(</span><span class="mi">1000</span><span class="o">,</span> <span class="mi">0</span><span class="o">,</span> <span class="mi">1400</span><span class="o">,</span> <span class="mi">0</span><span class="o">);</span>
        <span class="n">CanvasAidUtils</span><span class="o">.</span><span class="na">setLineColor</span><span class="o">(</span><span class="n">Color</span><span class="o">.</span><span class="na">GRAY</span><span class="o">);</span>
        <span class="n">CanvasAidUtils</span><span class="o">.</span><span class="na">draw2DCoordinateSpace</span><span class="o">(</span><span class="n">canvas</span><span class="o">);</span>
        <span class="n">canvas</span><span class="o">.</span><span class="na">restore</span><span class="o">();</span>
    <span class="o">}</span>

    <span class="cm">/**
     * 绘制平移后的坐标系，颜色为红色
     */</span>
    <span class="kd">private</span> <span class="kt">void</span> <span class="nf">drawTranslateCoordinateSpace</span><span class="o">(</span><span class="n">Canvas</span> <span class="n">canvas</span><span class="o">)</span> <span class="o">{</span>
        <span class="n">CanvasAidUtils</span><span class="o">.</span><span class="na">set2DAxisLength</span><span class="o">(</span><span class="mi">500</span><span class="o">,</span> <span class="mi">500</span><span class="o">,</span> <span class="mi">700</span><span class="o">,</span> <span class="mi">700</span><span class="o">);</span>
        <span class="n">CanvasAidUtils</span><span class="o">.</span><span class="na">setLineColor</span><span class="o">(</span><span class="n">Color</span><span class="o">.</span><span class="na">RED</span><span class="o">);</span>
        <span class="n">CanvasAidUtils</span><span class="o">.</span><span class="na">draw2DCoordinateSpace</span><span class="o">(</span><span class="n">canvas</span><span class="o">);</span>
        <span class="n">CanvasAidUtils</span><span class="o">.</span><span class="na">draw2DCoordinateSpace</span><span class="o">(</span><span class="n">canvas</span><span class="o">);</span>
    <span class="o">}</span>
<span class="o">}</span>
</code></pre>
</div>

<p>其实核心部分就这两点：</p>

<div class="language-java highlighter-rouge"><pre class="highlight"><code><span class="c1">// ▼ 注意此处使用 getRawX，而不是 getX</span>
<span class="n">down_x</span> <span class="o">=</span> <span class="n">event</span><span class="o">.</span><span class="na">getRawX</span><span class="o">();</span>
<span class="n">down_y</span> <span class="o">=</span> <span class="n">event</span><span class="o">.</span><span class="na">getRawY</span><span class="o">();</span>

<span class="c1">// -------------------------------------</span>

<span class="c1">// ▼ 获得当前矩阵的逆矩阵</span>
<span class="n">Matrix</span> <span class="n">invertMatrix</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Matrix</span><span class="o">();</span>
<span class="n">canvas</span><span class="o">.</span><span class="na">getMatrix</span><span class="o">().</span><span class="na">invert</span><span class="o">(</span><span class="n">invertMatrix</span><span class="o">);</span>

<span class="c1">// ▼ 使用 mapPoints 将触摸位置转换为画布坐标</span>
<span class="n">invertMatrix</span><span class="o">.</span><span class="na">mapPoints</span><span class="o">(</span><span class="n">pts</span><span class="o">);</span>
</code></pre>
</div>

<ol>
  <li>使用全局坐标系</li>
  <li>使用逆矩阵的 mapPoints</li>
</ol>

<p><strong>原理嘛，其实非常简单，我们在画布上正常的绘制，需要将画布坐标系转换为全局坐标系后才能真正的绘制内容。所以我们反着来，将获得到的全局坐标系坐标使用当前画布的逆矩阵转化一下，就转化为当前画布的坐标系坐标了，如果对 <a href="http://www.gcssloop.com/customview/Matrix_Basic">Matrix原理</a> 和  <a href="http://www.gcssloop.com/customview/Matrix_Method">Matrix详解</a>  理解了，即便我不说你们也肯定会想到这个方案的。</strong></p>

<h2 id="仿遥控器按钮代码示例">仿遥控器按钮代码示例</h2>

<p>在解决了上述两大难题之后，相信不论形状如何奇葩的自定义控件，基本上都难不倒大家了，最后用一个简单的示例作为结尾，还是文章开头所举的例子，核心内容就是上面讲的两个东西。</p>

<p><img src="http://gcsblog.oss-cn-shanghai.aliyuncs.com/blog/2019-04-29-071519.jpg?gcssloop" alt="" /></p>

<p>代码：</p>

<div class="language-java highlighter-rouge"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">RemoteControlMenu</span> <span class="kd">extends</span> <span class="n">CustomView</span> <span class="o">{</span>
    <span class="n">Path</span> <span class="n">up_p</span><span class="o">,</span> <span class="n">down_p</span><span class="o">,</span> <span class="n">left_p</span><span class="o">,</span> <span class="n">right_p</span><span class="o">,</span> <span class="n">center_p</span><span class="o">;</span>
    <span class="n">Region</span> <span class="n">up</span><span class="o">,</span> <span class="n">down</span><span class="o">,</span> <span class="n">left</span><span class="o">,</span> <span class="n">right</span><span class="o">,</span> <span class="n">center</span><span class="o">;</span>

    <span class="n">Matrix</span> <span class="n">mMapMatrix</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span>

    <span class="kt">int</span> <span class="n">CENTER</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span>
    <span class="kt">int</span> <span class="n">UP</span> <span class="o">=</span> <span class="mi">1</span><span class="o">;</span>
    <span class="kt">int</span> <span class="n">RIGHT</span> <span class="o">=</span> <span class="mi">2</span><span class="o">;</span>
    <span class="kt">int</span> <span class="n">DOWN</span> <span class="o">=</span> <span class="mi">3</span><span class="o">;</span>
    <span class="kt">int</span> <span class="n">LEFT</span> <span class="o">=</span> <span class="mi">4</span><span class="o">;</span>
    <span class="kt">int</span> <span class="n">touchFlag</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span><span class="o">;</span>
    <span class="kt">int</span> <span class="n">currentFlag</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span><span class="o">;</span>

    <span class="n">MenuListener</span> <span class="n">mListener</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span>

    <span class="kt">int</span> <span class="n">mDefauColor</span> <span class="o">=</span> <span class="mi">0</span><span class="n">xFF4E5268</span><span class="o">;</span>
    <span class="kt">int</span> <span class="n">mTouchedColor</span> <span class="o">=</span> <span class="mi">0</span><span class="n">xFFDF9C81</span><span class="o">;</span>


    <span class="kd">public</span> <span class="nf">RemoteControlMenu</span><span class="o">(</span><span class="n">Context</span> <span class="n">context</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">this</span><span class="o">(</span><span class="n">context</span><span class="o">,</span> <span class="kc">null</span><span class="o">);</span>
    <span class="o">}</span>

    <span class="kd">public</span> <span class="nf">RemoteControlMenu</span><span class="o">(</span><span class="n">Context</span> <span class="n">context</span><span class="o">,</span> <span class="n">AttributeSet</span> <span class="n">attrs</span><span class="o">)</span> <span class="o">{</span>
        <span class="kd">super</span><span class="o">(</span><span class="n">context</span><span class="o">,</span> <span class="n">attrs</span><span class="o">);</span>

        <span class="n">up_p</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Path</span><span class="o">();</span>
        <span class="n">down_p</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Path</span><span class="o">();</span>
        <span class="n">left_p</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Path</span><span class="o">();</span>
        <span class="n">right_p</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Path</span><span class="o">();</span>
        <span class="n">center_p</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Path</span><span class="o">();</span>

        <span class="n">up</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Region</span><span class="o">();</span>
        <span class="n">down</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Region</span><span class="o">();</span>
        <span class="n">left</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Region</span><span class="o">();</span>
        <span class="n">right</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Region</span><span class="o">();</span>
        <span class="n">center</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Region</span><span class="o">();</span>

        <span class="n">mDeafultPaint</span><span class="o">.</span><span class="na">setColor</span><span class="o">(</span><span class="n">mDefauColor</span><span class="o">);</span>
      	<span class="n">mDeafultPaint</span><span class="o">.</span><span class="na">setAntiAlias</span><span class="o">(</span><span class="kc">true</span><span class="o">);</span>

        <span class="n">mMapMatrix</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Matrix</span><span class="o">();</span>

    <span class="o">}</span>

    <span class="nd">@Override</span>
    <span class="kd">protected</span> <span class="kt">void</span> <span class="nf">onSizeChanged</span><span class="o">(</span><span class="kt">int</span> <span class="n">w</span><span class="o">,</span> <span class="kt">int</span> <span class="n">h</span><span class="o">,</span> <span class="kt">int</span> <span class="n">oldw</span><span class="o">,</span> <span class="kt">int</span> <span class="n">oldh</span><span class="o">)</span> <span class="o">{</span>
        <span class="kd">super</span><span class="o">.</span><span class="na">onSizeChanged</span><span class="o">(</span><span class="n">w</span><span class="o">,</span> <span class="n">h</span><span class="o">,</span> <span class="n">oldw</span><span class="o">,</span> <span class="n">oldh</span><span class="o">);</span>
        <span class="n">mMapMatrix</span><span class="o">.</span><span class="na">reset</span><span class="o">();</span>
		
      	<span class="c1">// 注意这个区域的大小</span>
        <span class="n">Region</span> <span class="n">globalRegion</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Region</span><span class="o">(-</span><span class="n">w</span><span class="o">,</span> <span class="o">-</span><span class="n">h</span><span class="o">,</span> <span class="n">w</span><span class="o">,</span> <span class="n">h</span><span class="o">);</span>
        <span class="kt">int</span> <span class="n">minWidth</span> <span class="o">=</span> <span class="n">w</span> <span class="o">&gt;</span> <span class="n">h</span> <span class="o">?</span> <span class="n">h</span> <span class="o">:</span> <span class="n">w</span><span class="o">;</span>
        <span class="n">minWidth</span> <span class="o">*=</span> <span class="mf">0.8</span><span class="o">;</span>

        <span class="kt">int</span> <span class="n">br</span> <span class="o">=</span> <span class="n">minWidth</span> <span class="o">/</span> <span class="mi">2</span><span class="o">;</span>
        <span class="n">RectF</span> <span class="n">bigCircle</span> <span class="o">=</span> <span class="k">new</span> <span class="n">RectF</span><span class="o">(-</span><span class="n">br</span><span class="o">,</span> <span class="o">-</span><span class="n">br</span><span class="o">,</span> <span class="n">br</span><span class="o">,</span> <span class="n">br</span><span class="o">);</span>

        <span class="kt">int</span> <span class="n">sr</span> <span class="o">=</span> <span class="n">minWidth</span> <span class="o">/</span> <span class="mi">4</span><span class="o">;</span>
        <span class="n">RectF</span> <span class="n">smallCircle</span> <span class="o">=</span> <span class="k">new</span> <span class="n">RectF</span><span class="o">(-</span><span class="n">sr</span><span class="o">,</span> <span class="o">-</span><span class="n">sr</span><span class="o">,</span> <span class="n">sr</span><span class="o">,</span> <span class="n">sr</span><span class="o">);</span>

        <span class="kt">float</span> <span class="n">bigSweepAngle</span> <span class="o">=</span> <span class="mi">84</span><span class="o">;</span>
        <span class="kt">float</span> <span class="n">smallSweepAngle</span> <span class="o">=</span> <span class="o">-</span><span class="mi">80</span><span class="o">;</span>

        <span class="c1">// 根据视图大小，初始化 Path 和 Region</span>
        <span class="n">center_p</span><span class="o">.</span><span class="na">addCircle</span><span class="o">(</span><span class="mi">0</span><span class="o">,</span> <span class="mi">0</span><span class="o">,</span> <span class="mf">0.2f</span> <span class="o">*</span> <span class="n">minWidth</span><span class="o">,</span> <span class="n">Path</span><span class="o">.</span><span class="na">Direction</span><span class="o">.</span><span class="na">CW</span><span class="o">);</span>
        <span class="n">center</span><span class="o">.</span><span class="na">setPath</span><span class="o">(</span><span class="n">center_p</span><span class="o">,</span> <span class="n">globalRegion</span><span class="o">);</span>

        <span class="n">right_p</span><span class="o">.</span><span class="na">addArc</span><span class="o">(</span><span class="n">bigCircle</span><span class="o">,</span> <span class="o">-</span><span class="mi">40</span><span class="o">,</span> <span class="n">bigSweepAngle</span><span class="o">);</span>
        <span class="n">right_p</span><span class="o">.</span><span class="na">arcTo</span><span class="o">(</span><span class="n">smallCircle</span><span class="o">,</span> <span class="mi">40</span><span class="o">,</span> <span class="n">smallSweepAngle</span><span class="o">);</span>
        <span class="n">right_p</span><span class="o">.</span><span class="na">close</span><span class="o">();</span>
        <span class="n">right</span><span class="o">.</span><span class="na">setPath</span><span class="o">(</span><span class="n">right_p</span><span class="o">,</span> <span class="n">globalRegion</span><span class="o">);</span>

        <span class="n">down_p</span><span class="o">.</span><span class="na">addArc</span><span class="o">(</span><span class="n">bigCircle</span><span class="o">,</span> <span class="mi">50</span><span class="o">,</span> <span class="n">bigSweepAngle</span><span class="o">);</span>
        <span class="n">down_p</span><span class="o">.</span><span class="na">arcTo</span><span class="o">(</span><span class="n">smallCircle</span><span class="o">,</span> <span class="mi">130</span><span class="o">,</span> <span class="n">smallSweepAngle</span><span class="o">);</span>
        <span class="n">down_p</span><span class="o">.</span><span class="na">close</span><span class="o">();</span>
        <span class="n">down</span><span class="o">.</span><span class="na">setPath</span><span class="o">(</span><span class="n">down_p</span><span class="o">,</span> <span class="n">globalRegion</span><span class="o">);</span>

        <span class="n">left_p</span><span class="o">.</span><span class="na">addArc</span><span class="o">(</span><span class="n">bigCircle</span><span class="o">,</span> <span class="mi">140</span><span class="o">,</span> <span class="n">bigSweepAngle</span><span class="o">);</span>
        <span class="n">left_p</span><span class="o">.</span><span class="na">arcTo</span><span class="o">(</span><span class="n">smallCircle</span><span class="o">,</span> <span class="mi">220</span><span class="o">,</span> <span class="n">smallSweepAngle</span><span class="o">);</span>
        <span class="n">left_p</span><span class="o">.</span><span class="na">close</span><span class="o">();</span>
        <span class="n">left</span><span class="o">.</span><span class="na">setPath</span><span class="o">(</span><span class="n">left_p</span><span class="o">,</span> <span class="n">globalRegion</span><span class="o">);</span>

        <span class="n">up_p</span><span class="o">.</span><span class="na">addArc</span><span class="o">(</span><span class="n">bigCircle</span><span class="o">,</span> <span class="mi">230</span><span class="o">,</span> <span class="n">bigSweepAngle</span><span class="o">);</span>
        <span class="n">up_p</span><span class="o">.</span><span class="na">arcTo</span><span class="o">(</span><span class="n">smallCircle</span><span class="o">,</span> <span class="mi">310</span><span class="o">,</span> <span class="n">smallSweepAngle</span><span class="o">);</span>
        <span class="n">up_p</span><span class="o">.</span><span class="na">close</span><span class="o">();</span>
        <span class="n">up</span><span class="o">.</span><span class="na">setPath</span><span class="o">(</span><span class="n">up_p</span><span class="o">,</span> <span class="n">globalRegion</span><span class="o">);</span>

    <span class="o">}</span>

    <span class="nd">@Override</span>
    <span class="kd">public</span> <span class="kt">boolean</span> <span class="nf">onTouchEvent</span><span class="o">(</span><span class="n">MotionEvent</span> <span class="n">event</span><span class="o">)</span> <span class="o">{</span>
        <span class="kt">float</span><span class="o">[]</span> <span class="n">pts</span> <span class="o">=</span> <span class="k">new</span> <span class="kt">float</span><span class="o">[</span><span class="mi">2</span><span class="o">];</span>
        <span class="n">pts</span><span class="o">[</span><span class="mi">0</span><span class="o">]</span> <span class="o">=</span> <span class="n">event</span><span class="o">.</span><span class="na">getRawX</span><span class="o">();</span>
        <span class="n">pts</span><span class="o">[</span><span class="mi">1</span><span class="o">]</span> <span class="o">=</span> <span class="n">event</span><span class="o">.</span><span class="na">getRawY</span><span class="o">();</span>
        <span class="n">mMapMatrix</span><span class="o">.</span><span class="na">mapPoints</span><span class="o">(</span><span class="n">pts</span><span class="o">);</span>

        <span class="kt">int</span> <span class="n">x</span> <span class="o">=</span> <span class="o">(</span><span class="kt">int</span><span class="o">)</span> <span class="n">pts</span><span class="o">[</span><span class="mi">0</span><span class="o">];</span>
        <span class="kt">int</span> <span class="n">y</span> <span class="o">=</span> <span class="o">(</span><span class="kt">int</span><span class="o">)</span> <span class="n">pts</span><span class="o">[</span><span class="mi">1</span><span class="o">];</span>

        <span class="k">switch</span> <span class="o">(</span><span class="n">event</span><span class="o">.</span><span class="na">getActionMasked</span><span class="o">())</span> <span class="o">{</span>
            <span class="k">case</span> <span class="n">MotionEvent</span><span class="o">.</span><span class="na">ACTION_DOWN</span><span class="o">:</span>
                <span class="n">touchFlag</span> <span class="o">=</span> <span class="n">getTouchedPath</span><span class="o">(</span><span class="n">x</span><span class="o">,</span> <span class="n">y</span><span class="o">);</span>
                <span class="n">currentFlag</span> <span class="o">=</span> <span class="n">touchFlag</span><span class="o">;</span>
                <span class="k">break</span><span class="o">;</span>
            <span class="k">case</span> <span class="n">MotionEvent</span><span class="o">.</span><span class="na">ACTION_MOVE</span><span class="o">:</span>
                <span class="n">currentFlag</span> <span class="o">=</span> <span class="n">getTouchedPath</span><span class="o">(</span><span class="n">x</span><span class="o">,</span> <span class="n">y</span><span class="o">);</span>
                <span class="k">break</span><span class="o">;</span>
            <span class="k">case</span> <span class="n">MotionEvent</span><span class="o">.</span><span class="na">ACTION_UP</span><span class="o">:</span>
                <span class="n">currentFlag</span> <span class="o">=</span> <span class="n">getTouchedPath</span><span class="o">(</span><span class="n">x</span><span class="o">,</span> <span class="n">y</span><span class="o">);</span>
            	<span class="c1">// 如果手指按下区域和抬起区域相同且不为空，则判断点击事件</span>
                <span class="k">if</span> <span class="o">(</span><span class="n">currentFlag</span> <span class="o">==</span> <span class="n">touchFlag</span> <span class="o">&amp;&amp;</span> <span class="n">currentFlag</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span> <span class="o">&amp;&amp;</span> <span class="n">mListener</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
                    <span class="k">if</span> <span class="o">(</span><span class="n">currentFlag</span> <span class="o">==</span> <span class="n">CENTER</span><span class="o">)</span> <span class="o">{</span>
                        <span class="n">mListener</span><span class="o">.</span><span class="na">onCenterCliched</span><span class="o">();</span>
                    <span class="o">}</span> <span class="k">else</span> <span class="k">if</span> <span class="o">(</span><span class="n">currentFlag</span> <span class="o">==</span> <span class="n">UP</span><span class="o">)</span> <span class="o">{</span>
                        <span class="n">mListener</span><span class="o">.</span><span class="na">onUpCliched</span><span class="o">();</span>
                    <span class="o">}</span> <span class="k">else</span> <span class="k">if</span> <span class="o">(</span><span class="n">currentFlag</span> <span class="o">==</span> <span class="n">RIGHT</span><span class="o">)</span> <span class="o">{</span>
                        <span class="n">mListener</span><span class="o">.</span><span class="na">onRightCliched</span><span class="o">();</span>
                    <span class="o">}</span> <span class="k">else</span> <span class="k">if</span> <span class="o">(</span><span class="n">currentFlag</span> <span class="o">==</span> <span class="n">DOWN</span><span class="o">)</span> <span class="o">{</span>
                        <span class="n">mListener</span><span class="o">.</span><span class="na">onDownCliched</span><span class="o">();</span>
                    <span class="o">}</span> <span class="k">else</span> <span class="k">if</span> <span class="o">(</span><span class="n">currentFlag</span> <span class="o">==</span> <span class="n">LEFT</span><span class="o">)</span> <span class="o">{</span>
                        <span class="n">mListener</span><span class="o">.</span><span class="na">onLeftCliched</span><span class="o">();</span>
                    <span class="o">}</span>
                <span class="o">}</span>
                <span class="n">touchFlag</span> <span class="o">=</span> <span class="n">currentFlag</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span><span class="o">;</span>
                <span class="k">break</span><span class="o">;</span>
            <span class="k">case</span> <span class="n">MotionEvent</span><span class="o">.</span><span class="na">ACTION_CANCEL</span><span class="o">:</span>
                <span class="n">touchFlag</span> <span class="o">=</span> <span class="n">currentFlag</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span><span class="o">;</span>
                <span class="k">break</span><span class="o">;</span>
        <span class="o">}</span>

        <span class="n">invalidate</span><span class="o">();</span>
        <span class="k">return</span> <span class="kc">true</span><span class="o">;</span>
    <span class="o">}</span>

  	<span class="c1">// 获取当前触摸点在哪个区域</span>
    <span class="kt">int</span> <span class="nf">getTouchedPath</span><span class="o">(</span><span class="kt">int</span> <span class="n">x</span><span class="o">,</span> <span class="kt">int</span> <span class="n">y</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">if</span> <span class="o">(</span><span class="n">center</span><span class="o">.</span><span class="na">contains</span><span class="o">(</span><span class="n">x</span><span class="o">,</span> <span class="n">y</span><span class="o">))</span> <span class="o">{</span>
            <span class="k">return</span> <span class="mi">0</span><span class="o">;</span>
        <span class="o">}</span> <span class="k">else</span> <span class="k">if</span> <span class="o">(</span><span class="n">up</span><span class="o">.</span><span class="na">contains</span><span class="o">(</span><span class="n">x</span><span class="o">,</span> <span class="n">y</span><span class="o">))</span> <span class="o">{</span>
            <span class="k">return</span> <span class="mi">1</span><span class="o">;</span>
        <span class="o">}</span> <span class="k">else</span> <span class="k">if</span> <span class="o">(</span><span class="n">right</span><span class="o">.</span><span class="na">contains</span><span class="o">(</span><span class="n">x</span><span class="o">,</span> <span class="n">y</span><span class="o">))</span> <span class="o">{</span>
            <span class="k">return</span> <span class="mi">2</span><span class="o">;</span>
        <span class="o">}</span> <span class="k">else</span> <span class="k">if</span> <span class="o">(</span><span class="n">down</span><span class="o">.</span><span class="na">contains</span><span class="o">(</span><span class="n">x</span><span class="o">,</span> <span class="n">y</span><span class="o">))</span> <span class="o">{</span>
            <span class="k">return</span> <span class="mi">3</span><span class="o">;</span>
        <span class="o">}</span> <span class="k">else</span> <span class="k">if</span> <span class="o">(</span><span class="n">left</span><span class="o">.</span><span class="na">contains</span><span class="o">(</span><span class="n">x</span><span class="o">,</span> <span class="n">y</span><span class="o">))</span> <span class="o">{</span>
            <span class="k">return</span> <span class="mi">4</span><span class="o">;</span>
        <span class="o">}</span>
        <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="o">;</span>
    <span class="o">}</span>

    <span class="nd">@Override</span>
    <span class="kd">protected</span> <span class="kt">void</span> <span class="nf">onDraw</span><span class="o">(</span><span class="n">Canvas</span> <span class="n">canvas</span><span class="o">)</span> <span class="o">{</span>
        <span class="kd">super</span><span class="o">.</span><span class="na">onDraw</span><span class="o">(</span><span class="n">canvas</span><span class="o">);</span>
        <span class="n">canvas</span><span class="o">.</span><span class="na">translate</span><span class="o">(</span><span class="n">mViewWidth</span> <span class="o">/</span> <span class="mi">2</span><span class="o">,</span> <span class="n">mViewHeight</span> <span class="o">/</span> <span class="mi">2</span><span class="o">);</span>

        <span class="c1">// 获取测量矩阵(逆矩阵)</span>
        <span class="k">if</span> <span class="o">(</span><span class="n">mMapMatrix</span><span class="o">.</span><span class="na">isIdentity</span><span class="o">())</span> <span class="o">{</span>
            <span class="n">canvas</span><span class="o">.</span><span class="na">getMatrix</span><span class="o">().</span><span class="na">invert</span><span class="o">(</span><span class="n">mMapMatrix</span><span class="o">);</span>
        <span class="o">}</span>

      	<span class="c1">// 绘制默认颜色</span>
        <span class="n">canvas</span><span class="o">.</span><span class="na">drawPath</span><span class="o">(</span><span class="n">center_p</span><span class="o">,</span> <span class="n">mDeafultPaint</span><span class="o">);</span>
        <span class="n">canvas</span><span class="o">.</span><span class="na">drawPath</span><span class="o">(</span><span class="n">up_p</span><span class="o">,</span> <span class="n">mDeafultPaint</span><span class="o">);</span>
        <span class="n">canvas</span><span class="o">.</span><span class="na">drawPath</span><span class="o">(</span><span class="n">right_p</span><span class="o">,</span> <span class="n">mDeafultPaint</span><span class="o">);</span>
        <span class="n">canvas</span><span class="o">.</span><span class="na">drawPath</span><span class="o">(</span><span class="n">down_p</span><span class="o">,</span> <span class="n">mDeafultPaint</span><span class="o">);</span>
        <span class="n">canvas</span><span class="o">.</span><span class="na">drawPath</span><span class="o">(</span><span class="n">left_p</span><span class="o">,</span> <span class="n">mDeafultPaint</span><span class="o">);</span>

      	<span class="c1">// 绘制触摸区域颜色</span>
        <span class="n">mDeafultPaint</span><span class="o">.</span><span class="na">setColor</span><span class="o">(</span><span class="n">mTouchedColor</span><span class="o">);</span>
        <span class="k">if</span> <span class="o">(</span><span class="n">currentFlag</span> <span class="o">==</span> <span class="n">CENTER</span><span class="o">)</span> <span class="o">{</span>
            <span class="n">canvas</span><span class="o">.</span><span class="na">drawPath</span><span class="o">(</span><span class="n">center_p</span><span class="o">,</span> <span class="n">mDeafultPaint</span><span class="o">);</span>
        <span class="o">}</span> <span class="k">else</span> <span class="k">if</span> <span class="o">(</span><span class="n">currentFlag</span> <span class="o">==</span> <span class="n">UP</span><span class="o">)</span> <span class="o">{</span>
            <span class="n">canvas</span><span class="o">.</span><span class="na">drawPath</span><span class="o">(</span><span class="n">up_p</span><span class="o">,</span> <span class="n">mDeafultPaint</span><span class="o">);</span>
        <span class="o">}</span> <span class="k">else</span> <span class="k">if</span> <span class="o">(</span><span class="n">currentFlag</span> <span class="o">==</span> <span class="n">RIGHT</span><span class="o">)</span> <span class="o">{</span>
            <span class="n">canvas</span><span class="o">.</span><span class="na">drawPath</span><span class="o">(</span><span class="n">right_p</span><span class="o">,</span> <span class="n">mDeafultPaint</span><span class="o">);</span>
        <span class="o">}</span> <span class="k">else</span> <span class="k">if</span> <span class="o">(</span><span class="n">currentFlag</span> <span class="o">==</span> <span class="n">DOWN</span><span class="o">)</span> <span class="o">{</span>
            <span class="n">canvas</span><span class="o">.</span><span class="na">drawPath</span><span class="o">(</span><span class="n">down_p</span><span class="o">,</span> <span class="n">mDeafultPaint</span><span class="o">);</span>
        <span class="o">}</span> <span class="k">else</span> <span class="k">if</span> <span class="o">(</span><span class="n">currentFlag</span> <span class="o">==</span> <span class="n">LEFT</span><span class="o">)</span> <span class="o">{</span>
            <span class="n">canvas</span><span class="o">.</span><span class="na">drawPath</span><span class="o">(</span><span class="n">left_p</span><span class="o">,</span> <span class="n">mDeafultPaint</span><span class="o">);</span>
        <span class="o">}</span>
        <span class="n">mDeafultPaint</span><span class="o">.</span><span class="na">setColor</span><span class="o">(</span><span class="n">mDefauColor</span><span class="o">);</span>
    <span class="o">}</span>

    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">setListener</span><span class="o">(</span><span class="n">MenuListener</span> <span class="n">listener</span><span class="o">)</span> <span class="o">{</span>
        <span class="n">mListener</span> <span class="o">=</span> <span class="n">listener</span><span class="o">;</span>
    <span class="o">}</span>

   	<span class="c1">// 点击事件监听器</span>
    <span class="kd">public</span> <span class="kd">interface</span> <span class="nc">MenuListener</span> <span class="o">{</span>
        <span class="kt">void</span> <span class="nf">onCenterCliched</span><span class="o">();</span>

        <span class="kt">void</span> <span class="nf">onUpCliched</span><span class="o">();</span>

        <span class="kt">void</span> <span class="nf">onRightCliched</span><span class="o">();</span>

        <span class="kt">void</span> <span class="nf">onDownCliched</span><span class="o">();</span>

        <span class="kt">void</span> <span class="nf">onLeftCliched</span><span class="o">();</span>
    <span class="o">}</span>
<span class="o">}</span>
</code></pre>
</div>

<p><strong>运行效果:</strong></p>

<p>当手指在某一区域活动时，该区域会高亮显示，如果注册了监听器，点击某一区域会触发监听器回调。</p>

<h2 id="关于硬件加速的问题">关于硬件加速的问题</h2>

<p><strong>硬件加速是个好东西，但是处理不好会引起诸多问题，博主为了怕麻烦我一直关闭硬件加速。</strong></p>

<p>然而硬件加速在 Android 4.0 以上是默认开启的，这就导致了有好几位魔法师反馈测试结果和我的测试结果不同，我来简单说明一下硬件加速干了什么事情，以及这些文章中的锅是如何产生的，应该由谁来背。</p>

<p>我在 <a href="http://www.gcssloop.com/customview/Matrix_Basic">Matrix 原理</a> 中说过 Matrix 的作用： <strong>Matrix作用就是坐标映射。</strong> <br />
其核心功能就是将单个 View 的坐标系转化为屏幕(物理)坐标系，虽然转换一次费不了多少时间，但是当执行动画效果等需要大量快速重绘的情况下，耗费的时间就需要考量一下了，于是乎，硬件加速干了一件非常<strong>精明</strong>的事情，<strong>把所有画布坐标系都设置为屏幕(物理)坐标系</strong>，之后在 View 绘制区域设置一个遮罩，保证绘制内容不会超过 View 自身的大小，<strong>这样就直接跳过坐标转换过程，可以节省坐标系之间数值转换耗费的时间</strong>。因此导致了以下问题：</p>

<ol>
  <li>开启硬件加速情况下 event.getX() 和 不开启情况下 event.getRawX() 等价，获取到的是屏幕(物理)坐标 (本文的锅)。</li>
  <li>开启硬件加速情况下 event.getRawX() 数值是一个错误数值，因为本身就是全局的坐标又叠加了一次 View 的偏移量，所以肯定是不正确的 (本文的锅)。</li>
  <li>从 Canvas 获取到的 Matrix 是全局的，默认情况下 x,y 偏移量始终为0，因此你不能从这里拿到当前 View 的偏移量 ( Matrix系列文章中的锅 )。</li>
  <li>由于其使用的是遮罩来控制绘制区域，所以如果重绘 path 时，如果 path 区域变大，但没有执行单步操作会导致 path 绘制不完整或者看起来比较奇怪 (Path系列文章中的锅)。</li>
</ol>

<p>很显然，这个硬件加速有点6，制造了各种锅想让我来背，然而智慧的我早已看穿一切，默默的把硬件加速关闭了，因为我不知道它还有多少锅没亮出来。</p>

<p><strong>这里顺便挖个坑，等我搞明白硬件加速扔锅的逻辑之后，专门写一篇硬件加速的文章，把硬件加速的锅全埋进去，再也不背这口大黑锅了</strong>。<br />
<strong>(╯°Д°）╯︵ ┻━┻</strong></p>

<p><strong>个人建议：</strong></p>

<ol>
  <li>APP全局关闭硬件加速。</li>
  <li>针对动画较多的 Activity 或者 View 单独开启硬件加速。</li>
  <li>如果应用要兼容到 3.0 以下，不要使用硬件加速的特性，或者进行兼容处理。</li>
  <li>如果 自定义View 出现与绘图相关的异常，请务必检查一下硬件加速。</li>
  <li>如果想关掉硬件加速看这里： <a href="https://github.com/GcsSloop/AndroidNote/issues/7">Android如何关闭硬件加速</a> 。</li>
</ol>

<h2 id="总结">总结</h2>

<p>本文虽然代码比较多，但核心概念非常简单，主要涉及以下两点：</p>

<ol>
  <li>Region 的区域检测。</li>
  <li>Matrix 的坐标映射。</li>
</ol>

<p><strong>这两个知识点都不是很难，然而灵活运用起来却是非常强大的，如果有对 Matrix 不了解的小伙伴，推荐去看我 <a href="http://www.gcssloop.com/customview/CustomViewIndex">之前的文章</a>，里面有关于Matrix的详细介绍，</strong></p>

<p>由于个人水平有限，文章中可能会出现错误，如果你觉得哪一部分有错误，或者发现了错别字等内容，欢迎在评论区告诉我，另外，据说关注 <a href="http://weibo.com/GcsSloop">作者微博</a> 不仅能第一时间收到新文章消息，还能变帅哦。</p>

<h2 id="about-me">About Me</h2>

<h3 id="作者微博-gcssloop">作者微博: <a href="http://weibo.com/GcsSloop" target="_blank">@GcsSloop</a></h3>

<p><a href="http://www.gcssloop.com/info/about" target="_blank"><img src="http://gcsblog.oss-cn-shanghai.aliyuncs.com/blog/gcs_banner.jpg?gcssloop" width="300" style="display:inline;" /></a></p>


    <hr>
  </section>
</article>

<!--广告-->

<!--
<div>
<a href="https://m.aliyun.com/act/team1111/?spm=5176.11533457.1089570.5.424777e3AF8WOJ&userCode=hn5smxtw#/" target="_blank"><img src="/assets/aliyun/1111-980-120.jpg" width="100%"></a>
</div>
-->
<!--捐赠晶石-->
<section class="contribute">
    <script type="text/javascript"> 
      function toggle() {
        var con = document.getElementById("contribute");
        if (con.style.display == "none") {
          con.style.display = "block";
        } else {
          con.style.display = "none";
        }
      }
    </script> 
    <blockquote style="background-color:#F5F5F5; padding: 10px 20px 20px 10px; margin:0px" >
      <h4> 如果你觉得我的文章对你有帮助的话，欢迎赞助一些服务器费用! </h4>
      <p></p>
      <a id=“btn-toggle-contribute” class="btn-contribute" onclick="toggle()" >¥ 点击赞助</a>
      <br>
      <div id="contribute" style="display:none;">
        <p align="center" >
        <img src="/assets/images/wechat.png" alt="微信">
        <img src="/assets/images/alipay.png" alt="支付宝">
        </p>
        <p align="left" >
          <b>感谢所有支持我的魔法师，所有支持过我的魔法师都可以通过微信(GcsSloop)联系我，获赠我的付费专栏！</b>
          <!--
          <a href="/contribute">点击这里查看捐赠者名单。</a>
          -->
        </p>
      </div>
    </blockquote>
</section>
<div>
  <h2>欢迎关注我的微信公众号</h2>
  <img src="/assets/images/banner.jpg" width="100%">
</div>

<!--阅读更多-->
<section class="read-more">
  
  
  <div class="read-more-item">
    <span class="read-more-item-dim">最近的文章</span>
    <h2 class="post-list__post-title post-title"><a href="/lecture/gdg-developer-growth-guide" title="link to 程序员练级指北">程序员练级指北</a></h2>
    <p class="excerpt">之前非常有幸收到 脉脉不得语 的邀请参加 郑州GDG1 举办的 DevFest2 活动，并上台分享了一下自己的拙见，回来之后我将自己演讲的内容整理了一下，并分享给大家，希望对一些人有帮助，关于 ...&hellip;</p>
    <div class="post-list__meta">
      <time datetime="2016-11-28 00:00:00 +0800" class="post-list__meta--date date">2016-11-28</time> &#8226; <span class="post-list__meta--tags tags">Lecture</span>
      <br/><br/>
      <a style="float:none; margin:0 auto;" class="btn-border-small" href=/lecture/gdg-developer-growth-guide>继续阅读</a></div>
   </div>
   
   
   
   
   <div class="read-more-item">
       <span class="read-more-item-dim">更早的文章</span>
       <h2 class="post-list__post-title post-title"><a href="/markdown/markdown-links" title="link to Markdown实用技巧－链接和图片">Markdown实用技巧－链接和图片</a></h2>
       <p class="excerpt">Sloop 喝过半杯咖啡，涨红的脸色渐渐复了原，旁人便又问道，“ Sloop，你当真会写文章么？” Sloop 看着问他的人，显出不屑置辩的神气。他们便接着说道，“你怎的连半个赞也捞不到呢？” ...&hellip;</p>
       <div class="post-list__meta">
          <time datetime="2016-11-08 00:00:00 +0800" class="post-list__meta--date date">2016-11-08</time> &#8226; <span class="post-list__meta--tags tags">Markdown</span>
          <br/><br/>
          <a style="float:none; margin:0 auto;" class="btn-border-small" href=/markdown/markdown-links>继续阅读</a>
       </div>
   </div>
   
</section>

<!--网易云跟帖-->
<!--
<div id="cloud-tie-wrapper" class="cloud-tie-wrapper"></div>
<script src="https://img1.cache.netease.com/f2e/tie/yun/sdk/loader.js"></script>
<script>
var cloudTieConfig = {
  url: document.location.href, 
  sourceId: "",
  productKey: "a85dba2840134721a7b69a15b2e0f217",
  target: "cloud-tie-wrapper"
};
var yunManualLoad = true;
Tie.loader("aHR0cHM6Ly9hcGkuZ2VudGllLjE2My5jb20vcGMvbGl2ZXNjcmlwdC5odG1s", true);
</script>
-->

<style type="text/css">
.isso-comment > div.avatar {
    border: 0px;
    box-shadow: none;
    display: block;
    float: left;
    width: 7%;
    margin: 3px 15px 0 0;
}
.isso-postbox > .form-wrapper > .auth-section .post-action > input {
    border-radius: 6px;
    padding: 6px;
    padding-left: 16px;
    padding-right: 16px;
    border: 1px solid #CCC;
    background-color: #D58D44;
    cursor: pointer;
    outline: 0;
    color: #fff;
    size: 10;
    line-height: 1.4em;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}
.isso-postbox > .form-wrapper > .auth-section .post-action > input:hover {
    background-color: #272822;
}
.isso-postbox > .form-wrapper > .auth-section .post-action > input:active {
    background-color: #986530;
}
</style>

<section id="isso-thread"></section>

<script data-isso="//47.52.58.34:1234/"
        data-isso-css="true"
        data-isso-lang="zh"
        data-isso-reply-to-self="false"
        data-isso-require-author="false"
        data-isso-require-email="false"
        data-isso-max-comments-top="10"
        data-isso-max-comments-nested="5"
        data-isso-reveal-on-click="5"
        data-isso-avatar="true"
        data-isso-avatar-bg="#f0f0f0"
        data-isso-avatar-fg="#9abf88 #5698c4 #e279a3 #9163b6 ..."
        data-isso-vote="true"
        data-vote-levels=""
        src="//47.52.58.34:1234/js/embed.min.js">
        </script>

<!--
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
-->
<!-- OneV's Den -->
<!--
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-3324997515191619"
     data-ad-slot="9170309685"
     data-ad-format="auto"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
-->

            <section class="footer">
    <footer>
    	<span class="footer__copyright">本站点采用<a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/deed.zh" target="_blank">知识共享 署名-非商业性使用-禁止演绎 4.0 国际 许可协议</a></span>
        <span class="footer__copyright">本站由 <a href="http://www.GcsSloop.com">@GcsSloop</a> 创建，采用 <a href="https://github.com/GcsSloop/Gcs-Vno-Jekyll" target="_blank">Gcs-Vno-Jekyll</a> 作为主题。<span id="busuanzi_container_site_pv"> 总访问量 <span id="busuanzi_value_site_pv"></span> 次</span> - &copy; 2019</span>
        <span class="footer__sitemap, footer__copyright"><a href="http://www.gcssloop.com/sitemap.xml" target="_blank">Site Map</a>
        <a href="http://www.gcssloop.com/vip" target="_blank">vip</a></span>
    </footer>
</section>

        </div>
    </div>
    
    <script type="text/javascript" src="//code.jquery.com/jquery-1.11.3.min.js"></script>

<script type="text/javascript" src="/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>

<script type="text/javascript" src="/js/main.js"></script>

<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-82493667-1', 'auto');
  ga('send', 'pageview');

</script>

    
  </body>

</html>
